#!/usr/bin/perl -w

$REPO=".git";
$CACHE="cache";

$CONF{'version'}='0.0.1';
$CONF{'version-name'}='Pre-dawn';
$CONF{'store-backend'}='git';
$CONF{'store-backend-repo'}='.git';
$CONF{'cache-backend'}='dir';
$CONF{'cache-backend-dir'}='cache';

$CONF{'refs/heads'}='refs/heads';

sub print_env {
    print "<tt>\n";
    foreach $key (sort keys(%ENV)) {
	print "$key = $ENV{$key}<br/>";
    }
    print "</tt>"
}

sub exit_404 {
    my($object) = @_;
    print "Content-type: text/plain\n\n";
    
    print "Error: 404\n";
    print "Object not found: '".$object."'\n";
    exit;
}

$cmd="";

if(defined $ENV{PATH_INFO}){
    $object=$ENV{PATH_INFO};
    $object=~s/^\///;
}else{
    $object="";
}

if($object =~ /\//){
    $object =~ s/^([^\/]+)\/(.*)$/$1/;
    $cmd = $2;
}

if( $object eq "" || $object eq "help"){
    print "Content-type: text/html\n\n";

    print "<pre>\n";
    print "Applestore\n";
    print "This system can be navigated by using the following links:\n";
    print "\n";
    print "<a href=\"help\">help</a>             - this information\n";
    print "<a href=\"refs-heads\">refs-heads</a>       - Document refererences\n";
    print "<a href=\"cache\">cache</a>            - display contents of cache\n";
    print "<a href=\"objects\">objects</a>          - display contents of object repository\n";
    print "<a href=\"configuration\">configuration</a>    - configuration information\n";  
    print "</pre>\n";
}elsif( $object eq "refs-heads" ){
    print "Content-type: text/html\n\n";

    @refs=`ls -1 $CONF{'refs/heads'}`;

    print "<h1>Object Heads</h1>\n";
    print "<table border=\"1\">";
    print "<tr>\n";
    print "<th>Head</th><th>Hash</th><th>Type</th>\n";
    print "</tr>\n";
    for $ref (@refs){
	chomp $ref;

	open(RFILE,"$CONF{'refs/heads'}/$ref");
	$ob=<RFILE>; chomp $ob;
	close RFILE;
	
	$object_type = `file -izb $CONF{'cache-backend-dir'}/$ob`;
	chomp $object_type;

	print "<tr>\n";
	print "<td><a href=\"$ob\">$ref</a></td>\n";
	print "<td>$ob</td>\n";
	print "<td>$object_type</td>\n";
	print "</tr>\n";
    }
    print "</table>\n";
}elsif( $object eq "cache" ){
    print "Content-type: text/html\n\n";

    @objects=`ls -1 $CONF{'cache-backend-dir'}`;
    print "<pre>";
    print "Object                                    mime type\n";
    print "----------------------------------------  ---------\n";
    for $ob (@objects){
	chomp $ob;
	print "<a href=\"$ob\">$ob</a>";
	$object_type = `file -izb $CONF{'cache-backend-dir'}/$ob`;
	chomp $object_type;
	print "  ";
	print "$object_type\n";
    }
    print "</pre>\n";
}elsif( $object eq "objects" ){
    print "Content-type: text/html\n\n";

    my($repo)="$CONF{'store-backend-repo'}/objects";
    $cmd="find $repo -type f";
    @objects=`$cmd`;
    print "<pre>";
    print "Object                                    object type\n";
    print "----------------------------------------  -----------\n";
    for $ob (@objects){
	chomp $ob;
	$ob =~ s/^$repo\///;
	$ob =~ s/\///g;

	print "<a href=\"$ob\">$ob</a>";

	$object_type = `git-cat-file -t $ob`;
	chomp $object_type;
	print "  ";
	print "$object_type";
	print "\n";
    }
    print "</pre>\n";

}elsif( $object eq "configuration" ){
    print "Content-type: text/plain\n\n";
    
    for $key (sort keys %CONF){
	printf("%-20s  %s\n",$key.":",$CONF{$key});
    }

}elsif( $object !~ /^.{40}$/ ){
    exit_404($object);
}else{

    # Fetch object not in cache
    if(! -e "cache/$object"){
	$metatype=`git-cat-file -t $object`; chomp $metatype;
	`git-cat-file $metatype $object > cache/$object`;
    }

    $object_type = `file -izb cache/$object`;
    chomp $object_type;

    print "Content-type: $object_type\n\n";

    open(IFILE,"cache/$object");
    while($line=<IFILE>){
	print $line;
    }
    close IFILE;
}
